home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / MCQUAY1 / SHADDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-14  |  3KB  |  101 lines

  1. program Shadow_Demo;
  2. { Very Simple Demonstration of Shadow Class Concepts
  3.      TPerson's FullName method places the name in Last,First
  4.      order.  This is not how we want it to behave.  We want
  5.      First Last order. So we create a Shadow Class with the
  6.      desired behavior.        }
  7. uses shadow;
  8. type
  9.     PPerson = ^TPerson;
  10.     TPerson = object
  11.         Last,First: string[40];
  12.         constructor init(FirstName,LastName:string);
  13.         destructor  done;
  14.         function    FullName:string; virtual 100;
  15.         end;
  16.  
  17. { This is the Shadow Class }
  18.     TPersonShadow = object(TPerson)
  19.         function FullName:string; virtual 100;
  20.         end;
  21.  
  22.     TRaceCar = object
  23.         Driver: PPerson;
  24.         Model: string[40];
  25.         Number: word;
  26.         constructor init(TheDriver:PPerson; MakeOfCar:string; CarNumber:word);
  27.         destructor  done;
  28.         function    Roster:string;
  29.         end;
  30.  
  31.     {--------------------------------}
  32.         constructor TPerson.init(FirstName,LastName:string);
  33.             begin
  34.             First := FirstName;
  35.             Last := LastName;
  36.             end;
  37.     {--------------------------------}
  38.         destructor  Tperson.done;
  39.             begin
  40.             { abstract method }
  41.             end;
  42.     {--------------------------------}
  43.         function Tperson.FullName:string;
  44.             begin
  45.             FullName := Last + ', '+First;
  46.             end;
  47.     {--------------------------------}
  48.         function TPersonShadow.FullName:string;
  49.             begin
  50.             FullName := First+' '+Last;
  51.             end;
  52.     {--------------------------------}
  53.         constructor TRaceCar.init(TheDriver:PPerson; MakeOfCar:string; CarNumber:word);
  54.             begin
  55.             if VALIDVMT(typeof(TheDriver^)) then
  56.                 begin
  57.                 Driver := TheDriver;
  58.                 Model := MakeOfCar;
  59.                 Number := CarNumber;
  60.                 end
  61.             else
  62.                 fail;
  63.             end;
  64.     {--------------------------------}
  65.         destructor  TraceCar.done;
  66.             begin
  67.             { Here just in case clean up is required }
  68.             end;
  69.     {--------------------------------}
  70.         function    TRaceCar.Roster:string;
  71.             var Temp: string[5];
  72.             begin
  73.             str(Number,Temp);
  74.             Roster:='Car Number '+Temp+', a '+Model+', is driven by '+
  75.                              driver^.FullName+'.';
  76.             end;
  77.     {--------------------------------}
  78. var
  79.     Person: PPerson;
  80.     Car: TRaceCar;
  81. begin
  82. { Create an Object of TRaceCar and PPerson Class }
  83.     Person := new(PPerson, init('Jenny','Quay'));
  84.     Car.init(Person,'Nissan Stanza',1);
  85. { Use their methods }
  86.     writeln('-- Original FullName Method --');
  87.     writeln(Car.Roster);
  88.     writeln(Person^.FullName);
  89. { Now replace the FULLNAME method with Shadow Method }
  90.     if ReplaceMethod (typeof(TPerson),@TPerson.FullName,@TPersonShadow.FullName)
  91.      then writeln('-- Shadow Class Installed --')
  92.      else writeln('-- Shadow Class NotInstalled --');
  93. { Now use the new Shadow Method }
  94.     writeln('-- Shadow FullName Method --');
  95.     writeln(Car.Roster);
  96.     writeln(Person^.FullName);
  97. { Clean Up }
  98.     Car.done;
  99.     dispose(Person ,done);
  100. end.
  101.